﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using TrenitaliaWP7.Resources;

namespace TrenitaliaWP7
{
    public class Result
    {

        public List<Details> solutions;
        public string nextQuery;
        public string buyQuery;
        //public string query;
        public Result()
        {
            nextQuery = "";
            buyQuery = "";
            solutions = new List<Details>();
        }

    }
    public class Details
    {
        public string buy_query;
        public DateTime lastUpdate;
        public string from;
        public string to;
        public string departure;
        public string arrival;
        public string duration;
        public List<Change> changes;
        public string number;
        public string category;
        public string price1;
        public string price2;
        public List<Train> trains;
        public Details()
        {
            from = "";
            to = "";
            departure = "";
            arrival = "";
            duration = "";
            changes = new List<Change>();
            number = "";
            category = "";
            price1 = "";
            price2 = "";
            trains = new List<Train>();
            lastUpdate = DateTime.Now;
            buy_query = "";
        }
        public Details(string from, string to, string departure, string arrival,
            string duration, string number, string category, string price1, string price2)
        {
            this.from = from;
            this.to = to;
            this.departure = departure;
            this.arrival = arrival;
            this.duration = duration;
            changes = new List<Change>();
            this.number = number;
            this.category = category;
            this.price1 = price1;
            this.price2 = price2;
            trains = new List<Train>();
            lastUpdate = DateTime.Now;
        }
        public bool isEqual(Details newFav)
        {
            if (number != newFav.number)
                return false;
            if (changes.Count != newFav.changes.Count)
                return false;
            for (int j = 0; j < changes.Count; j++)
                if (changes[j].number != newFav.changes[j].number)
                {
                    return false;
                }
            return true;
        }
        public override string ToString()
        {
            return ToString(0) + " " + ToString(1);
        }

        public string ToString(int i)
        {
            if (i == 0)
                return from + " (" + departure + ") --> " + to + " (" + arrival + ")";
            if (i == 1)
                return Strings.Duration + duration + (changes.Count > 0 ? " " + Strings.Changes + changes.Count : "");
            return "";
        }
        public string ToSMS()
        {
            var s = Strings.Train + number + ": " + from + " ( " + departure + " ) -> " + to + " ( " + arrival + " ) ";

            if (changes.Count >= 1)
                s += " - " + Strings.Changes + changes.Count;

            for (int i = 0; i < changes.Count; i++)
                s += "\n" + Strings.ChangeTrain + changes[i].number;

            return s;
        }
        public string ToEMailSubject()
        {
            var s = Strings.AppTitle + " - " + Strings.Train + number + ": " + from + " ( " + departure + " ) -> " + to + " ( " + arrival + " ) ";

            if (changes.Count >= 1)
                s += " - " + Strings.Changes + changes.Count;

            return s;
        }

        public string ToEMail()
        {
            var s = from + " ( " + departure + " ) -> " + to + " ( " + arrival + " ) ";

            if (changes.Count >= 1)
                s += " - " + Strings.Changes + changes.Count;
            s += "\n";
            s += "\n" + Strings.Duration + duration;
            s += "\n";
            if (price1 != "")
                s += "\n" + Strings.Price1 + price1 + " €";
            if (price1 != "")
                s += "\n" + Strings.Price2 + price2 + " €";
            s += "\n";
            for (int j = 0; j < trains.Count; j++)
            {
                var new_train = "";
                var t = trains[j];
                new_train = t.number + ": " + t.type + (t.delay != "" && t.delay != "-" ? " - " + Strings.Delay + " " + t.delay : "");

                for (int i = 0; i < t.stops.Count; i++)
                {
                    var new_stop = t.stops[i];
                    //int l1 = new_stop.Length;
                    new_stop += tabs(new_stop) + " " + t.times[i] + (t.real_times[i] != t.times[i] && t.real_times[i]!="" ? " (" + t.real_times[i] + ")" : "");
                    //int l2 = new_stop.Length;

                    //new_stop += (l2 - l1 <= 7 ? "\t" : "\t") + " " + t.binary[i] +
                    new_stop += " " + Strings.Binary + ": " + t.binary[i] +
                        (t.real_binary[i] != "" && t.real_binary[i] != "--" ? " (" + t.real_binary[i] + ")" : "");

                    if (t.cancelled[i])
                        new_stop += " - " + Strings.Cancelled + "!";

                    new_train += "\n" + new_stop;
                }


                s += "\n" + new_train;
                if(t.status!="")
                    s += "\n\n" + Strings.LastStatus + t.status;

                
            }
            s += "\n\n" + Strings.LastUpdate + "\n" + lastUpdate.ToString();
            return s;
        }

        private string tabs(string val)
        {
            int letters = val.Length;
            //string s = "";
            //for (int i = letters; i < max_spaces; i+=8)
            //    s += "\t";
            if (letters <= 9)
                return "\t\t\t";
            if (letters <= 20)
                return "\t\t";
            if (letters <= 30)
                return "\t";
            return "";
        }


        public string ToEMail_Webpage()
        {
            /*
            var s = from + " ( " + departure + " ) -> " + to + " ( " + arrival + " ) ";

            if (changes.Count >= 1)
                s += " - " + Strings.Changes + changes.Count;
            s += "\n";
            s += "\n" + Strings.Duration + duration;
            s += "\n";
            if (price1 != "")
                s += "\n" + Strings.Price1 + price1 + " €";
            if (price1 != "")
                s += "\n" + Strings.Price2 + price2 + " €";
            s += "\n";
            for (int j = 0; j < trains.Count; j++)
            {
                var new_train = "";
                var t = trains[j];
                new_train = t.number + ": " + t.type + (t.delay != "" && t.delay != "-" ? " - " + Strings.Delay + " " + t.delay : "");

                for (int i = 0; i < t.stops.Count; i++)
                {
                    var new_stop = t.stops[i];
                    //int l1 = new_stop.Length;
                    new_stop += tabs(new_stop) + " " + t.times[i] + (t.real_times[i] != t.times[i] && t.real_times[i] != "" ? " (" + t.real_times[i] + ")" : "");
                    //int l2 = new_stop.Length;

                    //new_stop += (l2 - l1 <= 7 ? "\t" : "\t") + " " + t.binary[i] +
                    new_stop += " " + Strings.Binary + ": " + t.binary[i] +
                        (t.real_binary[i] != "" && t.real_binary[i] != "--" ? " (" + t.real_binary[i] + ")" : "");

                    if (t.cancelled[i])
                        new_stop += " - " + Strings.Cancelled + "!";

                    new_train += "\n" + new_stop;
                }


                s += "\n" + new_train;
                if (t.status != "")
                    s += "\n\n" + Strings.LastStatus + t.status;


            }
            s += "\n\n" + Strings.LastUpdate + "\n" + lastUpdate.ToString();
             * 
             * */
            var s = "";
            s = "------=_NextPart_000_002C_01BFABBF.4A7D6BA0\n" +
                "Content-Type: text/html;\n" +
                "charset=\"iso-8859-1\"\n" +
                "Content-Transfer-Encoding: quoted-printable\n";
            s += "<html><body>";
            s += "<h1>TITLE</h1>";
            s += "</body></html>";
            s += "\n------=_NextPart_000_002C_01BFABBF.4A7D6BA0--";
            return s;
        }
    }
    public class Change
    {
        public string hour;
        public string where;
        public string category;
        public string number;
        public Change()
        {
            hour = "";
            where = "";
            category = "";
            number = "";
        }
        public Change(string hour, string where, string category, string number)
        {
            this.hour = hour;
            this.where = where;
            this.category = category;
            this.number = number;
        }
    }

    public class Train
    {
        public List<string> stops;
        public List<string> times;
        public List<string> real_times;
        public List<string> binary;
        public List<string> real_binary;
        public List<bool> cancelled;
        public string delay;
        public string number;
        public string type;
        public string status;
        public Train()
            : this("", "")
        { }
        public Train(string number, string type)
        {
            stops = new List<string>();
            times = new List<string>();
            real_times = new List<string>();
            binary = new List<string>();
            real_binary = new List<string>();
            cancelled = new List<bool>();
            delay = "";
            this.type = type;
            this.number = number;
            status = "";
        }
    }


    public class Favorites
    {
        public List<Details> favorites;
        public Favorites()
        {
            favorites = new List<Details>();
        }
        public void AddFavorite(Details newFav)
        {
            RemoveFavorite(newFav);
            favorites.Insert(0, newFav);
        }
        public bool IsFavorite(Details chosen)
        {
            for (int i = 0; i < favorites.Count; i++)
                if (favorites[i].isEqual(chosen))
                    return true;
            return false;
        }
        public void RemoveFavorite(Details chosen)
        {
            int itemToRemove = -1;
            for (int i = 0; i < favorites.Count; i++)
                if (favorites[i].isEqual(chosen))
                {
                    itemToRemove = i;
                    break;
                }
            if (itemToRemove >= 0 && itemToRemove < favorites.Count)
                favorites.RemoveAt(itemToRemove);
        }
        public List<Train> GetFavouriteTrains(Details chosen)
        {
            //List<Train> trains = ;
            //if (!IsFavorite(chosen))
            //    return trains;
            for (int i = 0; i < favorites.Count; i++)
                if (favorites[i].isEqual(chosen))
                    return favorites[i].trains;

            return new List<Train>();
        }
    }

    public class SavedResults
    {
        public string name;
        public List<Result> results;
        public SavedResults()
        {
            name = "";
            results = new List<Result>();
        }
    }
}
